﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using Radiation___IM;

namespace RadiationUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            Connection.IP = ipBox.Text;
            Connection.Port = (int)portSelector.Value;

            try
            {
                string old_text = this.LoginButton.Text;
                this.LoginButton.Text = "Sécurisation ...";
                this.LoginButton.Enabled = false;

                Radiation___IM.RSAManager.Init();
                AESManager.Init();
                this.LoginButton.Text = old_text;

                Connection.Client = new TcpClient();
                Connection.Client.Connect(Connection.IP, Connection.Port);
                Connection.Client.Client.NoDelay = true;

                Main main = new Main();
                if (!main.ErrorLoading)
                {
                    main.FormClosed += new FormClosedEventHandler(main_FormClosed);
                    main.Show();

                    this.Hide();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Impossible de se connecter au serveur", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                this.LoginButton.Text = "Connexion";
                this.LoginButton.Enabled = true;
            }
        }

        void main_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Show();
        }
    }
}
